package com.zztl.infiniteBand.ui.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.zztl.infiniteBand.util.UiUtils


/**
 * @desc
 * @auth ${user}
 * @time 2018/2/27  10:39
 */

class SideBar : View {
    /**
     * 触摸事件
     */
    private var onTouchingLetterChangedListener: OnTouchingLetterChangedListener? = null
    private var ifSelected = -1 // 选中
    private val paint = Paint()


    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context) : super(context)

    /**
     * 重写这个方法
     */
    override fun onDraw(canvas: Canvas) {

        super.onDraw(canvas)
        // 获取焦点改变背景颜色.
        val height = height // 获取对应高度
        val width = width // 获取对应宽度
        val singleHeight = height / characters.size // 获取每一个字母的高度

        for (i in characters.indices) {

            // 设置颜色
            paint.color = Color.rgb(188, 184, 178)
            // 设置字体
            paint.typeface = Typeface.DEFAULT_BOLD
            // 设置抗锯齿
            paint.isAntiAlias = true
            // 设置字体大小
            paint.textSize = UiUtils.dip2px(context, 10).toFloat()
            // x坐标等于中间-字符串宽度的一半.
            val xPos = width / 2 - paint.measureText(characters[i]) / 2
            val yPos = (singleHeight * i + singleHeight).toFloat()
            // 选中的状态
            if (i == ifSelected) {
                val rect = Rect()
                paint.getTextBounds(characters[i], 0, characters[i].length, rect)
                val textHeight = rect.height()//文字高
                paint.color = Color.parseColor("#007aff")
                canvas.drawCircle((width / 2).toFloat(), (singleHeight * (i + 1) - textHeight / 2).toFloat(), UiUtils.dip2px(context, 8).toFloat(), paint)

                paint.color = Color.parseColor("#201500")
                paint.isFakeBoldText = true
            }


            canvas.drawText(characters[i], xPos, yPos, paint)
            paint.reset()// 重置画笔


        }
    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {

        val action = event.action
        val y = event.y// 点击y坐标
        val oldSelected = ifSelected
        val listener = onTouchingLetterChangedListener
        val selected = (y / height * characters.size).toInt()// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.

        when (action) {

            MotionEvent.ACTION_UP ->

                ifSelected = -1
            else ->

                if (oldSelected != selected) {
                    if (selected >= 0 && selected < characters.size) {
                        listener?.onTouchingLetterChanged(characters[selected])
                        ifSelected = selected
                        invalidate()
                    }
                }
        }//                invalidate();
        return true
    }

    /**
     * 向外公开的方法
     *
     * @param onTouchingLetterChangedListener
     */
    fun setOnTouchingLetterChangedListener(onTouchingLetterChangedListener: OnTouchingLetterChangedListener) {

        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener
    }

    /**
     * 触碰右侧列表时的回调接口
     */
    interface OnTouchingLetterChangedListener {
        fun onTouchingLetterChanged(s: String)
    }

    companion object {
        /**
         * 26个字母
         */
        var characters = arrayOf("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#")
    }
}
